package cn.wlh.learn.webmagic.pipeline;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.wlh.learn.webmagic.tool.AliOssClientTool;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;

/**
 * 抽象的转化管道。
 * 把原来单个from 经过具体操作（conversionObject），放到to里面去。
 * <pre>
 *     resultItems.get(this.from);
 *     resultItems.put(this.to,conversionObject)
 * </pre>
 */
public abstract class AbstractConversionPipeline implements Pipeline {
    private final static Log logger = LogFactory.get();
    /**
     * 来源key
     */
    private final String from;
    /**
     * 目的key
     */
    private final String to;
    /**
     * 设置读取和存入的key
     * @param from
     * @param to
     */
    public AbstractConversionPipeline(String from, String to) {
        this.from = from;
        this.to = to;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {

        Map<String, Object> all = resultItems.getAll();
        Object fromObj = all.get(this.from);
        Object toObj = null;
        //
        if(fromObj instanceof Collection){
            Collection collection =  ((Collection) fromObj);
            JSONArray jsonArray = new JSONArray(collection.size());
            //
            collection.forEach(x -> {
                //
                Object objectOSS = conversionOne(x);
                //
                jsonArray.add(objectOSS);
            });
            //
            toObj = jsonArray;
        // String
        }else{
            toObj = conversionOne(fromObj);
        }

        // 存入
        resultItems.put(this.to,toObj);
    }

    /**
     * 转化单个，对象供子类实现。
     * @param urlObj
     * @return
     */
    public abstract Object conversionOne(Object urlObj);


    public String getFrom() {
        return from;
    }

    public String getTo() {
        return to;
    }
}